﻿using AutoMan.Dtos;
using AutoMan.Handles;
using AutoMan.Helper;
using DevExpress.Utils;
using DevExpress.Xpo.Helpers;
using DevExpress.Xpo.Metadata;
using DevExpress.XtraLayout;
using DevExpress.XtraTreeList.Columns;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DevExpress.Utils.Diagnostics.HandleSpy;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace AutoMan.Pages.Componets
{
    public partial class ViewPage : DevExpress.XtraEditors.XtraUserControl
    {
        private EntityHandle entityHandle;
        private string tableName = "";
        private string parentTableName = "";
        private int treelistActive = 1;
        private QueryDto queryDto = new QueryDto();
        private List<dynamic> GridDatas = new List<dynamic>();
        private List<TreeListDto> TreeListDatas = new List<TreeListDto>();
        public ViewPage(string tableName, string parentTableName = null, QueryDto queryDto = null)
        {
            InitializeComponent();
            treeList1.CustomDrawNodeCell += treeList1_CustomDrawNodeCell;
            treeList1.FocusedNodeChanged += treeList1_FocusedNodeChanged;

            this.tableName = tableName;
            this.parentTableName = parentTableName;
            this.queryDto = queryDto;
            if (tableName == "FormFieldModel") simpleButton4.Visible = true;
            if (string.IsNullOrEmpty(this.parentTableName))
            {
                layoutControlItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
            else
            {
                using (entityHandle = new EntityHandle(this.parentTableName))
                {
                    TreeListDatas = entityHandle.TreeList();
                    TreeListHelper.TreeNode(treeList1, TreeListDatas, "名称", "编号", "父级编号");
                }
            }

            //设置多选
            gridView1.OptionsSelection.MultiSelect = true;
            gridView1.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;
            gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;

            #region 属性设置
            //是否允许右键自定义布局菜单
            layoutControl1.AllowCustomization = false;
            #endregion
            //禁止编辑
            gridView1.OptionsBehavior.Editable = false;

            RefreshTable(this.queryDto);
        }

        private void RefreshTable(QueryDto queryDto)
        {
            entityHandle = new EntityHandle(this.tableName);
            GridDatas = entityHandle.List(queryDto);
            gridControl1.DataSource = GridDatas;
        }
        private string elementName = null;
        private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            //确定列的名字不为空
            if (e.CellValue == elementName)
            {
                //设置背景色
                e.Appearance.BackColor = panelControl1.BackColor;
                //设置开启背景色
                e.Appearance.Options.UseBackColor = true;
                //设置前景色
                e.Appearance.ForeColor = panelControl1.ForeColor;
                //设置开启前景色
                e.Appearance.Options.UseForeColor = true;

            }
        }
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (treeList1.FocusedNode?.GetValue("名称") == null) return;
            treelistActive = int.Parse(treeList1.FocusedNode.GetValue("编号").ToString());
            elementName = treeList1.FocusedNode.GetValue("名称").ToString();
            var query = new QueryDto()
            {
                WhereString = queryDto.WhereString,
                Parameters = new { treelistActive },
            };
            RefreshTable(query);
        }


        private void simpleButton1_Click(object sender, EventArgs e)
        {
            EditDialog editDialog = new EditDialog();
            ControlsHelper controlHelper = new ControlsHelper(tableName, editDialog);
            controlHelper.InitForm();
            DialogResult result = DevExpress.XtraEditors.XtraDialog.Show(editDialog, $"新增数据", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                MessageBox.Show("添加成功");
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            entityHandle.InitDataTable(treelistActive);
            MessageBox.Show("数据库同步成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
    }
}
